package com.chromatic.competition.utils;

import androidx.annotation.NonNull;

import com.chromatic.competition.inteface.UserPostService;
import com.chromatic.competition.model.UserPost;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class UserPostManager {
    /**
     * 根据用户ID获取用户发布的帖子列表
     *
     * @param userId   用户ID
     * @param callback 回调接口，用于处理请求结果
     */
    public void getUserPostsByUserId(int userId, final UserPostCallback callback) {
        RetrofitClientUtil retrofitClientUtil = RetrofitClientUtil.getInstance();
        UserPostService userPostService = retrofitClientUtil.getPostService();

        Call<List<UserPost>> call = userPostService.getUserPostsByUserId(userId);
        call.enqueue(new Callback<List<UserPost>>() {
            @Override
            public void onResponse(@NonNull Call<List<UserPost>> call, @NonNull Response<List<UserPost>> response) {
                if (response.isSuccessful()) {
                    List<UserPost> userPosts = response.body();
                    if (userPosts != null) {
                        callback.onSuccess(userPosts);
                    } else {
                        callback.onFailure("User posts list is null");
                    }
                } else {
                    callback.onFailure("Error response code: " + response.code());
                }
            }

            @Override
            public void onFailure(@NonNull Call<List<UserPost>> call, @NonNull Throwable t) {
                callback.onFailure("Error: " + t.getMessage());
            }
        });
    }

    /**
     * 用户帖子回调接口
     */
    public interface UserPostCallback {
        void onSuccess(List<UserPost> userPosts);
        void onFailure(String errorMessage);
    }
}